package proyecto.testcasetool.presentation.editor.pages;

import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.MasterDetailsBlock;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import proyecto.testcasetool.presentation.actions.GenerateCodeAction;
import proyecto.testcasetool.presentation.details.DetailsPage;


public abstract class AbstractMasterDetailsBlock extends MasterDetailsBlock {

	protected FormToolkit toolkit;
	protected AbstractMasterPart masterPart;
	protected EditingDomain editingDomain;
	protected GenerateCodeAction genCodeAction;
	protected DetailsPage lastDetailsPage;
	
	private boolean initialMasterPartToolBarVisibility = false;
	
	/**
	 * Default constructor. 
	 * The block is orientable.
	 * @wbp.parser.constructor
	 */
	public AbstractMasterDetailsBlock() {
		super();
	}

	//Especifico relacion de sash
	public void createContent(IManagedForm managedForm, Composite parent) {
		super.createContent(managedForm, parent);
		sashForm.setWeights(new int[] {2,5});
	}
	
	
	/**
	 * {@inheritDoc}
	 * @see org.eclipse.ui.forms.MasterDetailsBlock#createMasterPart(org.eclipse.ui.forms.IManagedForm, org.eclipse.swt.widgets.Composite)
	 */
	protected final void createMasterPart(IManagedForm managedForm, Composite parent) {
		this.toolkit = managedForm.getToolkit();
		Composite masterContainer = managedForm.getToolkit().createComposite(parent);
		masterContainer.setLayout(FormLayoutFactory.createMasterGridLayout(false, 1));
		masterContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		// TODO Arreglo de layout
		//FormLayoutFactory.visualizeLayoutArea(masterContainer, 11);
		masterPart = createMasterPart(masterContainer);
		masterPart.setInitialMasterPartToolBarVisibility(initialMasterPartToolBarVisibility);
		managedForm.addPart(masterPart);
		
		Section section = masterPart.getSection();
		section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
		section.setLayoutData(new GridData(GridData.FILL_BOTH));
		//FormLayoutFactory.visualizeLayoutArea(section, 13);
		
		// Expandir el arbol con doble click
		masterPart.getModelViewer().addDoubleClickListener( new IDoubleClickListener() {
			
			@Override
			public void doubleClick(DoubleClickEvent event) {
				 TreeViewer viewer = (TreeViewer) event.getViewer();
				    IStructuredSelection thisSelection = (IStructuredSelection) event.getSelection(); 
				    Object selectedNode = thisSelection.getFirstElement();
				    ITreeItemContentProvider adapter = (ITreeItemContentProvider)getAdapterFactory().adapt(selectedNode, ITreeItemContentProvider.class);
				    if (adapter.hasChildren(selectedNode)){
				    	viewer.setExpandedState(selectedNode,
				        !viewer.getExpandedState(selectedNode));
				    }
			}
		});

	}

	/**
	 * Create contents of the Master part
	 * @param masterContainer the container of this contents
	 * @return a MasterPart with defined contents
	 */
	protected abstract AbstractMasterPart createMasterPart(Composite masterContainer);

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ui.forms.MasterDetailsBlock#createToolBarActions(org.eclipse.ui.forms.IManagedForm)
	 */
	protected void createToolBarActions(IManagedForm managedForm) {
		
		/**
		 * Lo de abajo no funciona por un bug en e4: https://bugs.eclipse.org/bugs/show_bug.cgi?id=366528
		
		//ToolBarManager manager = (ToolBarManager) managedForm.getForm().getToolBarManager();
		////toolkit.decorateFormHeading(managedForm.getForm().getForm());
		//IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IMenuService.class);
		//menuService.populateContributionManager(manager, "popup:formsToolBar");
		//manager.update(true);
		 **/
		
		// usar Actions para mostrar las cosas del menu
		
		IToolBarManager manager = managedForm.getForm().getToolBarManager();
		genCodeAction = new GenerateCodeAction();
		manager.add(genCodeAction);
		
		
//		final ScrolledForm form = managedForm.getForm();
//		if (showValidateAction) {
//			validateAction = new ValidateAction();
//			validateAction.setToolTipText("Generar diagrama de actividad del caso de uso");
////			validateAction.setImageDescriptor(EEFRuntimePlugin.getImageDescriptor(EEFRuntimePlugin.ICONS_16x16 + "validate.gif")); //$NON-NLS-1$
//			form.getToolBarManager().add(validateAction);
//		}
//		
//		if (orientable) {
//			Action haction = new Action("hor", IAction.AS_RADIO_BUTTON) {  //$NON-NLS-1$
//				public void run() {
//					sashForm.setOrientation(SWT.HORIZONTAL);
//					form.reflow(true);
//				}
//			};
//			haction.setChecked(true);
////			haction.setToolTipText(EEFRuntimeUIMessages.AbstractEEFMasterDetailsBlock_horizontal_layout);
////			haction.setImageDescriptor(EEFRuntimePlugin.getImageDescriptor(EEFRuntimePlugin.ICONS_16x16 + "th_horizontal.gif")); //$NON-NLS-1$
////			haction.setDisabledImageDescriptor(EEFRuntimePlugin.getImageDescriptor(EEFRuntimePlugin.ICONS_16x16 + "disabled/th_horizontal.gif")); //$NON-NLS-1$
//			form.getToolBarManager().add(haction);
//
//			Action vaction = new Action("ver", IAction.AS_RADIO_BUTTON) {  //$NON-NLS-1$
//				public void run() {
//					sashForm.setOrientation(SWT.VERTICAL);
//					form.reflow(true);
//				}
//			};
//			vaction.setChecked(false);
////			vaction.setToolTipText(EEFRuntimeUIMessages.AbstractEEFMasterDetailsBlock_vertical_layout);
////			vaction.setImageDescriptor(EEFRuntimePlugin.getImageDescriptor(EEFRuntimePlugin.ICONS_16x16 + "th_vertical.gif")); //$NON-NLS-1$
////			vaction.setDisabledImageDescriptor(EEFRuntimePlugin.getImageDescriptor(EEFRuntimePlugin.ICONS_16x16 + "disabled/th_vertical.gif")); //$NON-NLS-1$
//			form.getToolBarManager().add(vaction);
//		}
		if (additionalPageActions() != null) {
			for (Action additionalAction : additionalPageActions()) {
				managedForm.getForm().getToolBarManager().add(additionalAction);
			}
		}
	}

	/**
	 * @return the master part of the block
	 */
	public AbstractMasterPart getMasterPart() {
		return masterPart;
	}
		
	/**
	 * @return the lastDetailsPage
	 */
	public DetailsPage getLastDetailsPage() {
		return lastDetailsPage;
	}

	/**
	 * Defines the orientation of the block.
	 * @param orientation the orientation to set
	 */
	public void setOrientation(int orientation) {
		sashForm.setOrientation(orientation);
	}
		
	/**
	 * @return the adapterFactory
	 */
	public AdapterFactory getAdapterFactory() {
		return masterPart.getAdapterFactory();
	}

	/**
	 * @param adapterFactory the adapterFactory to set
	 */
	public void setAdapterFactory(AdapterFactory adapterFactory) {
		masterPart.setAdapterFactory(adapterFactory);
	}

	/**
	 * @return the editingDomain
	 */
	public EditingDomain getEditingDomain() {
		return editingDomain;
	}

	/**
	 * @param editingDomain the editingDomain to set
	 */
	public void setEditingDomain(EditingDomain editingDomain) {
		this.editingDomain = editingDomain;
	}

	/**
	 * @param input the input of the model viewer
	 */
	public void setInput(Object input) {
		masterPart.setInput(input);
	}

	/**
	 * @param filter add a filter to the model viewer
	 */
	public void addFilter(ViewerFilter filter) {
		masterPart.addFilter(filter);
	}
	
	/**
	 * @return the list of actions to add to the form toolbar
	 */
	protected List<Action> additionalPageActions() {
		// nothing to add
		return null;
	}

	/**
	 * set the master tool bar visibility
	 * @param value
	 */
	public void setInitialMasterPartToolBarVisibility(boolean value) {
		initialMasterPartToolBarVisibility = value;
	}
	
	public boolean isInitialMasterPartToolBarVisibility(){
		return initialMasterPartToolBarVisibility;
	}
}
